import functionnodes.FunctionNode;
import data.Value;

/**
 * @author loaner
 */
class functionnodes.FunctionNodeAdd extends FunctionNode {
	
	function FunctionNodeAdd() {
		super();
		funcname = "add";
	}
	
	public function reduceValues( scope:Object, args:Array):Value
	{
		//Every function is passed an array of Values
		//and from these, reduces them via some operator to a single return Value
	
		var value0:Value = Value( args[0]);
		//var value1:Value = Value( args[1]);
		var sumtype:Function;
		var sumvalue:Value;
		var xmlnodename:String;
		
		 
		var sum:Object = value0.raw();
		for( var i:Number = 1; i < args.length; i++)
		{
			sum += args[ i ].raw();
		}
		
		if( value0.type == Value.NUMBER )
		{
			sumtype = Value.NUMBER;
			xmlnodename = "symbol";			
		}
		else if( value0.type == Value.STRING )
		{
			sumtype = Value.STRING;
			xmlnodename = "q";
		}
		
		
		var newXML:XML = new XML( "<" + xmlnodename + ">" +  sum +
		"</" + xmlnodename + ">" );
		var newnode:XMLNode = XMLNode ( newXML.firstChild );
		
		sumvalue = new Value( sumtype, sum, newnode );
		
		return sumvalue;
	}
	
	public function reduceXMLtoXML( scope:Object, args:Array):XMLNode
	{
		return args[0];	
	}
}